package com.hmyd.hmyd_android.ui;

import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.dto.VerifyCode;
import com.hmyd.hmyd_android.utils.MyToast;
import com.hmyd.hmyd_android.utils.RegPatterns;
import com.hmyd.hmyd_android.utils.Utils;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

/**
 * 登录页面，忘记密码
 * 
 * @author wangc
 * 
 */
@ContentView(R.layout.activity_find_password)
public class FindPasswordActivity extends BasicActivity implements OnClickListener {

	private String mCodeId;

	private final int SEND_VERIFY_CODE = 0;
	private final int RESETPASSWORD = 1;

	// 标题栏
	@ViewInject(R.id.home_title)
	public View home_title;
	@ViewInject(R.id.home_title_txt)
	public TextView home_title_txt;
	@ViewInject(R.id.home_right)
	public TextView home_right;

	@ViewInject(R.id.register_phone)
	private EditText mRegisterPhone;

	@ViewInject(R.id.et_code)
	private EditText mEtCode;

	@ViewInject(R.id.btn_get_code)
	private Button mBtnGetCode;

	@ViewInject(R.id.password)
	private EditText mPassword;

	@ViewInject(R.id.register)
	private Button mRegister;

	@OnClick({ R.id.btn_get_code, R.id.register })
	public void onClick(View view) {

		switch (view.getId()) {
		case R.id.btn_get_code:// 获取验证码
			handler.postDelayed(runnable, 0);

			RequestParams params = new RequestParams();
			params.addBodyParameter("mobile", mRegisterPhone.getText().toString());

			onRequest(SEND_VERIFY_CODE, UrlConstant.FORGETPASSWORD, HttpMethod.POST, params);
			break;
		case R.id.register:// 注册

			if (Utils.regPatternsPassword(this, mPassword.getText().toString().trim())) {// 密码验证不通过

			} else {
				RequestParams params2 = new RequestParams();
				params2.addBodyParameter("mobile", mRegisterPhone.getText().toString().trim());
				params2.addBodyParameter("newPassword", mPassword.getText().toString().trim());
				params2.addBodyParameter("codeId", mCodeId);
				params2.addBodyParameter("verifyCode", mEtCode.getText().toString().trim());

				onRequest(RESETPASSWORD, UrlConstant.RESETPASSWORD, HttpMethod.POST, params2);
			}

			break;
		default:
			break;
		}

	}

	private TextWatcher watcher = new TextWatcher() {

		@Override
		public void afterTextChanged(Editable s) {
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			regPatterns();
		}
	};

	private void regPatterns() {
		if (!RegPatterns.MobilePattern.matcher(mRegisterPhone.getText().toString()).matches()) {// 手机验证不通过
			mBtnGetCode.setEnabled(false);
			mRegister.setEnabled(false);
		} else {
			if (second == 60) {
				mBtnGetCode.setEnabled(true);
			}

			String etCode = mEtCode.getText().toString().trim();
			String password = mPassword.getText().toString().trim();

			if (!TextUtils.isEmpty(etCode) && !TextUtils.isEmpty(password)
					&& !TextUtils.isEmpty(mRegisterPhone.getText().toString())) {
				mRegister.setEnabled(true);
			}
		}
	}

	private int second = 60;
	Handler handler = new Handler();
	Runnable runnable = new Runnable() {
		@Override
		public void run() {
			if (second >= 0) {
				mBtnGetCode.setText(second-- + "s");
				mBtnGetCode.setEnabled(false);
			} else {
				mBtnGetCode.setText(R.string.register_get_code);
				mBtnGetCode.setEnabled(true);

				second = 60;

				Message message = new Message();
				message.what = 1;
				handlerStop.sendMessage(message);
			}

			handler.postDelayed(this, 1000);
		}
	};

	final Handler handlerStop = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				handler.removeCallbacks(runnable);
				break;
			}
			super.handleMessage(msg);
		}
	};

	public void onPostSuccess(int postId, String result) {
		Gson gson = new Gson();
		switch (postId) {
		case SEND_VERIFY_CODE:
			java.lang.reflect.Type type = new TypeToken<VerifyCode>() {
			}.getType();
			VerifyCode verifyCode = gson.fromJson(result, type);

			mCodeId = verifyCode.codeId;

			break;
		case RESETPASSWORD:
			MyToast.showToast(this, "重置密码成功");

			finish();

			break;
		default:
			break;

		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

		if (handlerStop != null && handler != null && runnable != null) {
			Message message = new Message();
			message.what = 1;
			handlerStop.sendMessage(message);
		}
	}

	@Override
	protected void setParams() {
		mRegisterPhone.addTextChangedListener(watcher);
		mEtCode.addTextChangedListener(watcher);
		mPassword.addTextChangedListener(watcher);

		home_title.setOnTouchListener(touch);
		home_title_txt.setText("密码重置");

	}

	@Override
	protected void leftButton() {
		AppBasic.getInstance().finishCurrentActivity();
	}

	@Override
	protected void rightButton() {

	}

	@Override
	public void onPostFailure(int postId, String msg) {

	}

}
